package com.cms.base;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;




public class HibernateDAOImpl<T> implements BaseDAO<T> {
	
	private HibernateTemplate hibernateTemplate;
	

	//@Override
	public void save(T entity) {
		hibernateTemplate.save(entity);
	}

	//@Override
	public void update(T entity) {
		hibernateTemplate.update(entity);
	}

	//@Override
	public T find(Serializable id, Class<T> clazz) {
		return (T) hibernateTemplate.get(clazz, id);
	}

	//@Override
	public void delete(Serializable id, Class<T> clazz) {
		hibernateTemplate.delete(this.find(id, clazz));
	}
	
	//@Override
	public void delete(List<Serializable> ids, Class<T> clazz) {
		for(int i=0; i<ids.size(); i++){
			Serializable id = ids.get(i);
			this.delete(id, clazz);
			if(i % 30 == 0)
				this.getSession().flush();
		}
	}

	
	
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	@Autowired
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	public Session getSession(){
        return SessionFactoryUtils.getSession(hibernateTemplate.getSessionFactory(),true);
    }
}
